package exam.test11.q1;

public class MyTokenizer implements Tokenizer {

    private String buffer;		//save text
    private Token currentToken;	//save token extracted from next()

    /**
     *  Tokenizer class constructor
     *  The constructor extracts the first token and save it to currentToken
     *  **** please do not modify this part ****
     */
    public MyTokenizer(String text) {
    	buffer = text;		// save input text (string)
    	next();		// extracts the first token.
    }

    /**
     *  This function will find and extract PrintLinkList next token from {@code buffer} and
     *  save the token to {@code currentToken}.
     */
    public void next() {


        buffer = buffer.trim(); // remove whitespace

         if(buffer.isEmpty()) {
            currentToken = null;	// if there's no string left, set currentToken null and return
            return;
         }

        // ########## YOUR CODE STARTS HERE ##########
        String wordTmp = buffer;
        if (buffer.contains(" ")) {
             wordTmp = buffer.substring(0, buffer.indexOf(" "));
        }
        currentToken = workCheck(wordTmp);
       
        // ########## YOUR CODE ENDS HERE ##########
        
        // Remove the extracted token from buffer
        int tokenLen = currentToken.getValue().length();
        buffer = buffer.substring(tokenLen);
    }

    private Token workCheck(String word) {
        if (word.matches("\\d+")) {
            if (Integer.parseInt(word) % 2 == 0) {
                return new Token(word, Token.Type.EVEN_NUMBER);
            } else {
                return new Token(word, Token.Type.ODD_NUMBER);
            }
        } else if (word.matches("\\w+")) {
            if (word.length() > 4) {
                return new Token(word, Token.Type.LONG_WORD);
            } else {
                return new Token(word, Token.Type.SHORT_WORD);
            }
        } else if (word.matches("\\w+@\\w+\\.\\w+")) {
            return new Token(word, Token.Type.EMAIL);
        } else {
            String wordTmp = "";
            String type = null; // word, number
            for (int i = 0; i < word.length(); i++) {
                char ch = word.charAt(i);
                if (ch >= '0' && ch <= '9') {
                    if (type == null || type.equals("number")) {
                        type = "number";
                        wordTmp += ch;
                    } else {
                        break;
                    }
                } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
                    if (type == null || type.equals("word")) {
                        type = "word";
                        wordTmp += ch;
                    } else {
                        break;
                    }
                } else {
                    if (wordTmp.length() > 0) {
                        return workCheck(wordTmp);
                    } else {
                        return new Token(ch + "", Token.Type.UNKNOWN);
                    }
                }
            }
            return workCheck(wordTmp);
        }
    }

    /**
     *  returned the current token extracted by {@code next()}
     *  **** please do not modify this part ****
     *  
     * @return type: Token
     */
    public Token current() {
    	return currentToken;
    }

    /**
     *  check whether there still exists another tokens in the buffer or not
     *  **** please do not modify this part ****
     *  
     * @return type: boolean
     */
    public boolean hasNext() {
    	return currentToken != null;
    }

}